package com.nongfuspring.nongfu.controller;

import com.nongfuspring.nongfu.pojo.*;
import com.nongfuspring.nongfu.service.HujiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ：huad.xu
 * @date ：Created in 2022/4/4 21:16
 * @description：户籍接口类
 * @modified By：
 * @version: 1.0$
 */
@RestController
public class HujiController {
    @Autowired
    HujiService hujiService;

    @PostMapping("/addHuji")
    public ResultBean addHuji(@RequestBody Huji huji){
        Integer i = hujiService.addHuji(huji);
        if(i ==1){
            return new ResultBean(2000,"新增户籍成功");
        }else{
            return new ResultBean(500,"新增户籍失败");
        }
    }

    @PostMapping("/queryHujiList")
    public ResultBean queryHujiList(@RequestBody Huji huji){
        ArrayList<Huji> hujiArrayList = new ArrayList<>();
        hujiArrayList = hujiService.queryHujiList(huji);
        return new ResultBean(2000,hujiArrayList);
    }

    @PostMapping("/addUserToHuji")
    public ResultBean addUserToHuji(@RequestBody Transfer transfer){

        //查询是否有户号
        Integer homeIdOfNumbers= hujiService.countHomeId(transfer.getHomeId());
        if(homeIdOfNumbers == 1 ){
            //查询是否添加了用户
            Integer ii = hujiService.queryUserBynUmber(transfer.getUsername(),transfer.getUsernumber());
            if(ii == 1){
                Integer i = hujiService.addUserToHuji(transfer);
                if(i ==1 ){
                    return new ResultBean(2000,"迁入成功");
                }else{
                    return  ResultBean.fail("迁入失败");
                }
            }else{
                return new ResultBean(500,"还未添加用户，请先添加用户");
            }

        }else{
            return new ResultBean(500,"还未添加户籍，请先添加");
        }
    }

    @PostMapping("/removeUserOfHuji")
    public ResultBean removeUserOfHuji(@RequestBody Transfer transfer){

        //查询是否有户号
        Integer homeIdOfNumbers= hujiService.countHomeId(transfer.getHomeId());
        if(homeIdOfNumbers == 1 ){
            Integer ii = hujiService.queryUserBynUmber(transfer.getUsername(),transfer.getUsernumber());
            if(ii == 1){
                Integer i = hujiService.removeUserOfHuji(transfer.getUsername(),transfer.getUsernumber());
                if(i ==1 ){
                    return new ResultBean(2000,"迁出成功");
                }else{
                    return  ResultBean.fail("迁出失败");
                }
            }else{
                return new ResultBean(500,"用户不存在，请先添加用户");
            }

        }else{
            return new ResultBean(500,"户籍号输入错误，请重新输入");
        }
    }


    @GetMapping("/queryhujidetail")
    public ResultBean queryhujidetail(Integer homeId){
        ArrayList<UserHuji> userArrayList = new ArrayList<>();
        userArrayList = hujiService.queryhujidetail(homeId);
        return ResultBean.ok(userArrayList);
    }

}
